<template>
  <!-- 导入表 -->
  <el-dialog title="导入表" :visible.sync="visible" width="1100px" top="5vh" append-to-body>
    <el-form :model="queryParams" ref="queryFormRef" :inline="true">
      <el-form-item label="数据源" prop="dataName">
        <el-select v-model="queryParams.dataName" filterable placeholder="请选择/输入数据源名称" style="width: 200px">
          <el-option v-for="item in dataNameList" :key="item" :label="item" :value="item"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="表名称" prop="tableName">
        <el-input v-model="queryParams.tableName" placeholder="请输入表名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="表描述" prop="tableComment">
        <el-input v-model="queryParams.tableComment" placeholder="请输入表描述" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-table @row-click="clickRow" ref="tableRef" :data="dbTableList" @selection-change="handleSelectionChange" height="260px">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="tableName" label="表名称" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="tableComment" label="表描述" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column prop="updateTime" label="更新时间"></el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    </el-row>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleImportTable">确 定</el-button>
        <el-button @click="visible = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { listDbTable, importTable, getDataNames } from '@/api/tool/gen';
export default {
    //import引入的组件需要注入到对象中才能使用
    components: {},
    data() {
        //这里存放数据
        return {
          total: 0,
          visible:false,
          tables:[],
          dbTableList:[],
          queryParams: {
            pageNum: 1,
            pageSize: 10,
            dataName: '',
            tableName: '',
            tableComment: ''
          },
          dataNameList:[]
        };
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //生命周期 - 创建完成（可以访问当前this实例）
    created() {

    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {

    },
    //方法集合
    methods: {
      /** 查询参数列表 */
      show(dataName){
        this.getDataNameList();
        if (dataName) {
          this.queryParams.dataName = dataName;
        } else {
          this.queryParams.dataName = 'master';
        }
        this.getList();
        this.visible = true;
      },
      /** 单击选择行 */
      clickRow(row){
        // ele bug
        this.$refs.tableRef?.toggleRowSelection(row, false);
      },
      /** 多选框选中数据 */
      handleSelectionChange(selection){
        this.tables = selection.map(item => item.tableName);
      },
      /** 查询表数据 */
      async getList(){
        const res = await listDbTable(this.queryParams);
        this.dbTableList = res.rows;
        this.total = res.total;
      },
      /** 搜索按钮操作 */
      handleQuery(){
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.$refs.queryFormRef?.resetFields();
        this.handleQuery();
      },
      /** 导入按钮操作 */
      async handleImportTable(){
        const tableNames = this.tables.join(",");
        if (tableNames == "") {
          this.$modal.msgError("请选择要导入的表");
          return;
        }
        const res = await importTable({ tables: tableNames, dataName: this.queryParams.dataName });
        this.$modal.msgSuccess(res.msg);
        if (res.code === 200) {
          this.visible = false;
          this.$emit("ok");
        }
      },
      /** 查询多数据源名称 */
      async getDataNameList(){
        const res = await getDataNames()
        this.dataNameList = res.data;
      },
    },
}
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css类

</style>